// FFileSystemLocal.h (c) 2011 JV Software
//

//
// FFileLocal
//
class ENGINE_API FFileLocal : public FFile {
public:
	virtual void			Destroy( void ) { 
								if(_handle != NULL) {
									if(data != NULL) {
										free(data);
									}
									fclose( _handle ); 
									_handle = NULL;
								}
							};
	void					OpenFile( FILE *handle ) { _handle = handle; data = NULL; _bufLen = 0; };
	virtual void			Read( void *buffer, int len ) {
								if(fread( buffer, len, 1, _handle ) != 1) {
									engine->FatalError( TEXT("Read beyond buffer") );
								}
							}
	virtual void			*Read( int len ) { 
								if( len != _bufLen && data != NULL ) {
									free( data );
								}

								data = (byte *)malloc( len );
								Read( data, len );

								return data;
							}
	virtual int				Len( void ) { 
								int _curpos = Tell();
								int len = 0;

								Seek( FSEEK_END, 0 );
								len = Tell();
								Seek( FSEEK_SET, _curpos );

								return len;
							}
	virtual int				Tell( void ) { return ftell( _handle ); };
	virtual void			Seek( FSeekState_t state, int pos ) {
								fseek( _handle, pos, state );
							}
private:
	FILE					*_handle;
	byte					*data;
	int						_bufLen;
};

//
// FFileSystemLocal
//
class FFileSystemLocal : public FFileSystem {
public:
	virtual void			Init( void );
	virtual FFile			*OpenFile( FString path );
private:
	FArray<FString>			searchPaths;
};
